package ths

import (
	"gitee.com/gcom/stockdock/core/support"
	"github.com/jinzhu/gorm"
)

type IndustryThsEntity struct {
	support.SimpleEntity
	*IndustryQuoteThs
}

func (IndustryThsEntity) TableName() string {
	return "industry_ths"
}

type CompanyThsEntity struct {
	support.SimpleEntity
	*CompInfoThs
}

func (CompanyThsEntity) TableName() string {
	return "comp_ths"
}

type StockThsEntity struct {
	support.SimpleEntity
	*DailyQuoteThs
	Incode string // 行业代码
}

func (StockThsEntity) TableName() string {
	return "stock_ths"
}

type StockQuoteThsEntity struct {
	support.SimpleEntity
	*StockQuoteThs
}

func (StockQuoteThsEntity) TableName() string {
	return "stock_quote_ths"
}

type StockHistThsEntity struct {
	support.SimpleEntity
	*StockHistThs
}

func (StockHistThsEntity) TableName() string {
	return "stock_hist_ths"
}

// UpdateIndustryList 更新同花顺行业列表
func UpdateIndustryList(db *gorm.DB, entities []*IndustryThsEntity) (int64, error) {
	return support.BatchInsertOrUpdate(db, entities, true)
}

// GetIndustryList 查询同花顺行业列表
func GetIndustryList(db *gorm.DB) (result []*IndustryThsEntity, err error) {
	err = db.Model(IndustryThsEntity{}).Find(&result).Error
	return result, err
}

// UpdateStockThs 更新股票报价及所属行业（同花顺）
func UpdateStockThs(db *gorm.DB, entities []*StockThsEntity) (int64, error) {
	return support.BatchInsertOrUpdate(db, entities, true)
}

// UpdateStockThs 更新股票报价(不包含所属行业字段)（同花顺）
func UpdateStockThsQuote(db *gorm.DB, entities []*StockThsEntity) (int64, error) {
	return support.BatchInsertOrUpdateOf(db, entities, func(f *gorm.Field) bool {
		return f.Name != "Id" && f.Name != "Incode" && (len(f.Name) <= 6 || f.Name[:6] != "Create")
	})
}

// UpdateCompanyThs 更新上市企业信息（同花顺）
func UpdateCompanyThs(db *gorm.DB, entities []*CompanyThsEntity) (int64, error) {
	return support.BatchInsertOrUpdate(db, entities, true)
}

// UpdateStockIndex 更新股票指标（同花顺）
func UpdateStockIndex(db *gorm.DB, entities []*StockQuoteThsEntity) (int64, error) {
	return support.BatchInsertOrUpdate(db, entities, true)
}

func GetStockThsDailyQuotes(db *gorm.DB, code string) (result []*StockQuoteThsEntity, err error) {
	err = db.Model(&StockQuoteThsEntity{}).Order("id").Find(&result, "code=?", code).Error
	return result, err
}

func UpdateStockQuoteHist(db *gorm.DB, entities []*StockHistThsEntity) (int64, error) {
	return support.BatchInsertOrUpdate(db, entities, true)
}

func GetStockQuoteHistThs(db *gorm.DB, code string) (result []*StockHistThsEntity, err error) {
	err = db.Model(&StockHistThsEntity{}).Order("id").Find(&result, "code=?", code).Error
	return result, err
}

func DeleteStockQuoteHist(db *gorm.DB, code string) (int64, error) {
	_db := db.Delete(&StockHistThsEntity{}, "code = ?", code)
	return _db.RowsAffected, _db.Error
}

func GetCompany(db *gorm.DB, code string) (*CompanyThsEntity, error) {
	var com = CompanyThsEntity{}
	var err = db.Model(&com).First(&com, "code = ?", code).Error
	return &com, err
}

func GetCompanyList(db *gorm.DB, q *CompanyQuery) (result []*CompanyThsEntity, err error) {
	var sql = "1=1"
	var cond = []interface{}{""}
	if q.Name != "" {
		sql += " and (name like ? or comp_name like ? or used_names like ?)"
		v := "%" + q.Name + "%"
		cond = append(cond, v, v, v)
	}
	if q.Code != "" {
		sql += " and code like ?"
		cond = append(cond, "%"+q.Code+"%")
	}
	if q.Area != "" {
		sql += " and area like ?"
		cond = append(cond, "%"+q.Area+"%")
	}
	if q.Industry != "" {
		sql += " and industry = ?"
		cond = append(cond, q.Industry)
	}
	cond[0] = sql
	err = db.Model(&CompanyThsEntity{}).Limit(q.Ps).Offset(q.Pn*q.Ps).Find(&result, cond...).Error
	return result, err
}

func GetStocks(db *gorm.DB, q *StocksQuery) (result []*StockThsEntity, err error) {
	var sql = "1=1"
	var cond = []interface{}{""}
	if len(q.Codes) > 0 {
		sql += " and code in (?)"
		cond = append(cond, q.Codes)
	}
	if q.Industry != "" {
		sql += " and incode = ?"
		cond = append(cond, q.Industry)
	}
	cond[0] = sql
	err = db.Model(&StockThsEntity{}).Limit(q.Ps).Offset(q.Pn*q.Ps).Find(&result, cond...).Error
	return result, err

}

func GetStocksCodeAndName(db *gorm.DB, q *StocksQuery) (result []*StockCodeAndName, err error) {
	var sql = "1=1"
	var cond []interface{}
	if len(q.Codes) > 0 {
		sql += " and code in (?)"
		cond = append(cond, q.Codes)
	}
	if q.Industry != "" {
		sql += " and incode = ?"
		cond = append(cond, q.Industry)
	}
	err = db.Model(StockThsEntity{DailyQuoteThs: &DailyQuoteThs{}}).Select("code, name").Where(sql, cond...).
		Limit(q.Ps).Offset(q.Pn * q.Ps).Scan(&result).Error
	return result, err
}
